package com.zhyt.intelligentpipeline.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhyt.intelligentpipeline.model.CorrosionDetection;
import com.zhyt.intelligentpipeline.service.CorrosionDetectionService;
import com.zhyt.intelligentpipeline.vo.CorrosionDetectionVO;
import com.zhyt.intelligentpipeline.vo.CorrosionTypeVo;
import com.zhyt.intelligentpipeline.vo.PageVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/CorrosionDetection")
public class CorrosionDetectionController {
    @Resource
    private CorrosionDetectionService corrosionDetectionService;
    @GetMapping
    public List<CorrosionDetection> getAll(){
        return corrosionDetectionService.list();
    }
    //分页
    @GetMapping("page/{page}/num/{num}")
    public IPage<CorrosionDetection> getPage(@PathVariable Integer page, @PathVariable Integer num)
    {
        IPage<CorrosionDetection>pages=new Page<>(page,num);
        return corrosionDetectionService.page(pages);
    }

    //根据管线ID获取管段的所有腐蚀信息
    @GetMapping("pipeline/{id}")
    public List<CorrosionDetection> getCorrosionDetectionsByPipelineId(@PathVariable(value = "id")String pipelineId){
        return corrosionDetectionService.getCorrosionDetectionByPipelineID(pipelineId);
    }
    @GetMapping("pipeline/{id}/page/{page}/num/{num}")
    public List<CorrosionDetection> getCorrosionDetectionsByPipelineIdPage(@PathVariable(value = "id")String pipelineId,
                                                                           @PathVariable(value = "page") Integer page,
                                                                           @PathVariable(value = "num") Integer num ){
        List<CorrosionDetection> corrosionDetections = corrosionDetectionService.getCorrosionDetectionByPipelineID(pipelineId);
        int endValue = Math.min(page * num, corrosionDetections.size());
        return corrosionDetections.subList((page - 1) * num ,endValue);
    }

    @GetMapping("pipeline/{id}/pageInfo/num/{num}")
    public PageVo getPageInfo(@PathVariable(value = "id")String pipelineId,
            @PathVariable(value = "num")Integer num){
        int counts = corrosionDetectionService.getCorrosionDetectionsByCount(pipelineId);
        PageVo pageVo = new PageVo();
        pageVo.setAllValues(counts);
        pageVo.setEveryPageNums(num);
        pageVo.setPages((int)Math.ceil(counts / (double) num));
        return pageVo;
    }

    /**
     * 根据腐蚀ID获取腐蚀剩余强度（单个）
     * */
    @GetMapping("{id}/residualStrength")
    public CorrosionDetectionVO getResidualStrengthById(@PathVariable(value = "id")String id){
        //先模拟计算
        CorrosionDetectionVO corrosionDetectionVO = new CorrosionDetectionVO();
        corrosionDetectionVO.setResidualStrength(Math.random() % 300);
        corrosionDetectionVO.setId(id);
        Date date = Calendar.getInstance().getTime();
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String timeValue = sdf.format(date);
        corrosionDetectionVO.setDate(timeValue);
        return corrosionDetectionVO;
    }

    @PostMapping("residualStrength")
    public List<CorrosionDetectionVO> getResidualStrengthsByIds(@RequestBody List<String> ids){
        //先模拟计算
        List<CorrosionDetectionVO> corrosionDetectionVOS = new ArrayList<>();
        for (String id : ids) {
            CorrosionDetectionVO corrosionDetectionVO = new CorrosionDetectionVO();
            corrosionDetectionVO.setResidualStrength(Math.random() % 300);
            corrosionDetectionVO.setId(id);
            Date date = Calendar.getInstance().getTime();
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String timeValue = sdf.format(date);
            corrosionDetectionVO.setDate(timeValue);
            corrosionDetectionVOS.add(corrosionDetectionVO);
        }
        return corrosionDetectionVOS;
    }

    /**
     * 获取腐蚀缺陷的分类数量
     * */
    @GetMapping("type")
    public  List<CorrosionTypeVo> getCorrosionType(){
        List<CorrosionDetection> corrosionDetections = corrosionDetectionService.list();
        return corrosionDetections.stream().collect(Collectors.groupingBy(CorrosionDetection::getType))
                .entrySet().stream().map(item ->{
                    String name = item.getKey().trim();
                    Integer nums =item.getValue().size();
                    return new CorrosionTypeVo(name,nums);
                }).collect(Collectors.toList());
    }

    //修改
    @PutMapping
    public Boolean update(@RequestBody CorrosionDetection corrosionDetection)
    {
        return corrosionDetectionService.updateById(corrosionDetection);
    }
    /**
     * 删除腐蚀
     */
    @DeleteMapping("delete/{id}")//这里面的id是和形参中的id是相应的才能传过去
    public Boolean delete(@PathVariable String id)
    {
        return corrosionDetectionService.removeById(id);
    }

//
//    public static void main(String[] args) {
//        int page = 8;int num = 13;
//        int size = 100 ;
//        int endValue = Math.min(page * num, size);
//        List<Integer> values = new ArrayList<>();
//        for (int i = 0; i < size; i++) {
//            values.add(i);
//        }
//        System.out.println(values.size());
//        System.out.println(values.subList((page - 1) * num ,endValue));
//    }
}
